package com.mx.tool.intentionAction.impl;

import com.cronutils.descriptor.CronDescriptor;
import com.cronutils.model.Cron;
import com.cronutils.model.CronType;
import com.cronutils.model.definition.CronDefinition;
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.parser.CronParser;
import com.intellij.codeInsight.hint.HintManager;
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.LogicalPosition;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.psi.PsiElement;
import com.intellij.ui.JBColor;
import com.intellij.ui.LightweightHint;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.util.IncorrectOperationException;
import com.mx.tool.intentionAction.IAction;
import com.mx.tool.util.NotifyUtil;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.util.Locale;

/**
 * @Author: mengxiang.
 * @create: 2021-03-08 10:06
 * @Description:
 */
public class TaskMessageAction extends PsiElementBaseIntentionAction implements IAction {


    private static final CronDefinition CRON_DEFINITION = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ);
    private static final CronParser CRON_PARSER = new CronParser(CRON_DEFINITION);
    private static final CronDescriptor DESCRIPTOR = CronDescriptor.instance(Locale.getDefault());


    @Nls
    @NotNull
    @Override
    public String getFamilyName() {
        return "任务表达式解析";
    }

    @Nls
    @NotNull
    @Override
    public String getText() {
        return "任务表达式解析";
    }

    @Override
    public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
        int start = editor.getSelectionModel().getSelectionStart();
        int end = editor.getSelectionModel().getSelectionEnd();
        String selectedText = editor.getDocument().getText().substring(start, end);
        if(StringUtils.isBlank(selectedText)){
            NotifyUtil.error(MESSAGE);
            return;
        }
        String description =getDescription(selectedText);
        showPopup(editor, description);
    }

    private void showPopup(Editor editor, String message) {
        JLabel label = new JLabel(message);
        label.setOpaque(true);
        label.setBackground(JBColor.LIGHT_GRAY);
        label.setBorder(BorderFactory.createLineBorder(JBColor.DARK_GRAY));

        // 获取光标位置并计算提示框显示的位置
        LogicalPosition logicalPosition = editor.getCaretModel().getLogicalPosition();
        Point point = editor.logicalPositionToXY(logicalPosition);
        RelativePoint relativePoint = new RelativePoint(editor.getContentComponent(), new Point(point.x, point.y - editor.getLineHeight()));

        // 使用 JBPopupFactory 创建并显示提示框
        JBPopupFactory.getInstance()
                .createComponentPopupBuilder(label, null)
                .setRequestFocus(false)
                .setCancelOnClickOutside(true)
                .createPopup()
                .show(relativePoint);
    }

    @Override
    public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
        return true;
    }


    public static String getDescription(String cronExpression) {
        Cron cron = CRON_PARSER.parse(cronExpression);
        // 这里可以自定义描述格式
        return DESCRIPTOR.describe(cron); // 这将返回原始表达式，需进一步处理以生成可读描述
    }
}
